package io.sage.modules.goods.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import io.sage.common.utils.CheckNullUtils;
import io.sage.common.utils.StringUtils;
import io.sage.rfid.config.RfidConfig;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.sage.common.utils.R;
import io.sage.modules.goods.entity.BGoods;
import io.sage.modules.goods.service.BGoodsService;
import io.sage.modules.sys.entity.SysUserEntity;
import io.sage.modules.sys.shiro.ShiroUtils;

/**
 * 
 *
 * @author xujh
 * @email jhang_mail@163.com
 * @date 2019-07-09 13:19:37
 */
@RestController
@RequestMapping("goods/bgoods")
public class BGoodsController {
	
    @Autowired
    private BGoodsService bGoodsService;
    @Autowired
    private RfidConfig rfidConfig;

	/**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("goods:bgoods:list")
    public R list(@RequestParam Map<String, Object> params){
    	Integer pageSize = 1;
		if (params != null && params.get("limit") != null) {
			pageSize = java.lang.Integer.parseInt(params.get("limit").toString());
		}
		Integer pageNum = 10;
		if (params != null && params.get("page") != null) {
			pageNum = java.lang.Integer.parseInt(params.get("page").toString());
		}

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		if(params.get("goodsName")!=null){

			if(!"".equals(params.get("goodsName").toString())){
				paramMap.put("goodsName", params.get("goodsName").toString());
			}
		}
		if(params.get("wareHouseId")!=null){

			if(!"".equals(params.get("wareHouseId").toString())){
				paramMap.put("wareHouseId", params.get("wareHouseId").toString());
			}
		}
		if(params.get("goodsAreaId")!=null){

			if(!"".equals(params.get("goodsAreaId").toString())){
				paramMap.put("goodsAreaId", params.get("goodsAreaId").toString());
			}
		}
		if(params.get("goodsShelveId")!=null){

			if(!"".equals(params.get("goodsShelveId").toString())){
				paramMap.put("goodsShelveId", params.get("goodsShelveId").toString());
			}
		}
		if(params.get("allocationId")!=null){

			if(!"".equals(params.get("allocationId").toString())){
				paramMap.put("allocationId", params.get("allocationId").toString());
			}
		}



		PageHelper.startPage(pageNum, pageSize);// 设置分页的起始码以及页面大小
		List<BGoods> selectGoodsList = bGoodsService.selectGoodsList(paramMap);

		PageInfo<BGoods> pageInfo = new PageInfo<BGoods>(selectGoodsList);// 传入list就可以了

		return R.ok().put("goodsList", pageInfo);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("goods:bgoods:info")
    public R info(@PathVariable("id") Integer id){
    	BGoods goods = bGoodsService.selectByPrimaryKey(id);
		Map<String, Object> paramMap = new HashMap<String, Object>();

		paramMap.put("goods", goods);

		return R.ok().put("map", paramMap);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("goods:bgoods:save")
	public R save(@RequestBody JSONObject jsonObject){
    	BGoods goods = jsonObject.getObject("goods", BGoods.class);

		SysUserEntity user = ShiroUtils.getUserEntity();
		
		goods.setCreateUserId(user.getUserId().intValue());
		
		int insert = bGoodsService.insert(goods);

		if(insert>0){
			return R.ok();
		}

		return R.error();
	}
    /**
     * 读取电子标签
     */
    @RequestMapping("/readLabelErp")
   //@RequiresPermissions("goods:bgoods:save")
	public R readLabelErp(@RequestBody JSONObject jsonObject){
    	/*BGoods goods = jsonObject.getObject("goods", BGoods.class);

		SysUserEntity user = ShiroUtils.getUserEntity();

		goods.setCreateUserId(user.getUserId().intValue());

		int insert = bGoodsService.insert(goods);*/


			return R.ok().put("labelErp","123456");


		/*return R.error();*/
	}
	/*
	*批量跟新 上架信息
	 */
    @RequestMapping("/updateUpShelfBatch")
   //@RequiresPermissions("goods:bgoods:save")
	public R updateUpShelfBatch(@RequestBody List<BGoods>list){
    	int i=this.bGoodsService.updateUpShelfBatch(list);
		if(i>0){
			return R.ok();
		}

		return R.error();
	}

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("goods:bgoods:update")
	public R update(@RequestBody JSONObject jsonObject){
    	BGoods goods = jsonObject.getObject("goods", BGoods.class);
		
		if(goods.getId()==null){
			return R.error("需要修改的id，不能为空");
		}
		int update = bGoodsService.updateByPrimaryKeySelective(goods);//全部更新

		if(update>0){
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
    @RequiresPermissions("goods:bgoods:delete")
	public R delete(@RequestBody Integer[] ids){

		int delete = bGoodsService.deleteBatchIds(ids);

		if(delete>0){
			return R.ok();
		}

		return R.error();
	}



}
